<?php

namespace App\Console\Commands;

use Alipayopen\Sdk\AopClient;
use Alipayopen\Sdk\Request\AlipayCommerceIotDeviceBindRequest;
use App\Models\RefundOrder;
use App\Services\PengYiPeng\SPILiteService;
use App\Util\LiteErrorResponse;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Test\Test;
use function foo\func;

class PengyiPengCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'pengyyipeng';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'pengyyipeng';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle(): void
    {

//        $url = 'https://qr.alipay.com/ibx162834byojovww3jchf7?deviceId=LDN7HDQM024100905169%2526supplyid%253D202008061702346558%2526biztid%253D01010QV0hq5XHtCpzQmGlOrPljB95Lw7XX9xmA9EbKcrewlxNOlaj0U%2526token%253Dd3641380231865c54adf0f9d34413098';
//        $url = urldecode(urldecode($url));
//
//        "https://qr.alipay.com/ibx162834byojovww3jchf7?deviceId=LDN7HDQM024100905169&supplyid=202008061702346558&biztid=01010QV0hq5XHtCpzQmGlOrPljB95Lw7XX9xmA9EbKcrewlxNOlaj0U&token=d3641380231865c54adf0f9d34413098"
        $store_id = "202289152247162424";
        $store_name = "合肥市蜀山区鑫飞电子经营部";
        $sn = "LDN7HDQM024100905169";
//
//        $store_id = "202442895849811363";
//        $store_name = "合肥市蜀山区鑫飞电子经营部";
//        $sn = "LDN7HDQM124102109011";

        //先查询
        list($bool, $ext_params, $terminal_bind_info) = $this->query($sn);
        $external_shop_id = "";
        if (!empty($terminal_bind_info)) {
            $store_id = $terminal_bind_info["store_id"];
            $store_name = $terminal_bind_info["store_name"];
        }
        if (!empty($ext_params)) {
            $external_shop_id = (string)$ext_params["external_shop_id"] ?? "";
        }
        $this->delete($sn, $store_id, $store_name, $external_shop_id);
    }

    protected function delete(string $sn, string $store_id, string $store_name, string $external_shop_id)
    {
        $service = new SPILiteService();
        list($bool, $result) = $service->deviceSet("IOT_DEVICE_RECORDS_DELETE", "DELETE", [
            "sn" => $sn,
            "RECORDS_TYPE_SMID" => "2088050311283937",  //-- 2088420011056520: 鑫飞
            "store_id" => $store_id, //小于等于15
            "store_name" => $store_name,
            "user_id" => 1,
            "external_shop_id" => $external_shop_id,
        ]);
        dd($bool, $result);
    }


    protected function query(string $sn): array
    {
        $service = new SPILiteService();
        list($bool, $result) = $service->deviceSet("IOT_DEVICE_RECORDS_QUERY", "QUERY", [
            "sn" => $sn,//LDN7HDQM024100905169
//            "RECORDS_TYPE_SMID" => "2088420011056520",  //-- 2088420011056520: 鑫飞
//            "store_id" => $store_id, //小于等于15
//            "store_name" => $store_name,
//            "user_id" => 1
        ]);
        $result = json_decode(json_encode($result), true);
        $ext_params = $result["device_records"]["ext_params"] ?? [];
        $terminal_bind_info = json_decode($ext_params["terminal_bind_info"] ?? "", true);
        return [$bool, $ext_params, $terminal_bind_info];
//        dd($bool, $result, $ext_params, $terminal_bind_info);
    }

    protected function bind(string $sn, string $store_id, string $store_name, string $external_shop_id)
    {
        $service = new SPILiteService();
        list($bool, $result) = $service->deviceSet("IOT_DEVICE_RECORDS_G2", "BIND", [
            "sn" => $sn,
            "RECORDS_TYPE_SMID" => "2088050311283937",  //-- 2088420011056520: 鑫飞
            "store_id" => $store_id, //小于等于15
            "store_name" => $store_name,
            "external_shop_id" => $external_shop_id,
            "user_id" => 1
        ]);
        dd($bool, $result);
    }

}
